package validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import command.BasicInfoForm;
import command.SearchForm;

@Component
public class SearchFormValidator implements Validator {
	
	@Override
	public boolean supports(Class<?> clazz) {
		return BasicInfoForm.class.isAssignableFrom(clazz);
	}
	
	@Override
	public void validate(Object target, Errors errors) {
		SearchForm form = (SearchForm) target;
		if (form.getSavedSearchName().trim().isEmpty()) {
			errors.rejectValue("savedSearchName", "empty");
		} else if (form.getSavedSearchName().length() > 50) {
			errors.rejectValue("savedSearchName", "tooLong");
		}
	}
}
